------------------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  E:\REStat_MS14767_Vol96(2)\Data preparation Compustat\11_patentpanel.log
  log type:  text
 opened on:  18 Dec 2014, 17:00:15

. *-----------------------------------------------------------------
. 
. use "raw data\apat63_99.dta"

. so assignee

. drop if gyear<1969
(349072 observations deleted)

. 
. merge assignee using "patent files\cusipnamemerged_red.dta"
variable assignee does not uniquely identify observations in the master data

. 
. tab _merge 

     _merge |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |  1,925,439       74.78       74.78
          3 |    649,411       25.22      100.00
------------+-----------------------------------
      Total |  2,574,850      100.00

. 
. keep if _merge==3
(1925439 observations deleted)

. drop _merge

. 
. *************************
. * Check observations
. count if assignee!=assignee[_n-1]
 3590

. * must give number around 3590
. ***************************
. 
. so ticker gyear

. drop if ticker==""
(97729 observations deleted)

. 
. keep patent gyear assignee ticker 

. 
. compress
  ticker was str8 now str7
  (551,682 bytes saved)

. 
. gen t=1

. egen numpatentsy=sum(t), by (ticker gyear)

. 
. drop t patent assignee 

. 
. 
. gen year=1969

. save "patent files\patent1969.dta", replace
file patent files\patent1969.dta saved

. local i=69

. forvalues i=70/99 {
  2. replace year=19`i'
  3. save "patent files\patent19`i'.dta", replace
  4. }
(551682 real changes made)
file patent files\patent1970.dta saved
(551682 real changes made)
file patent files\patent1971.dta saved
(551682 real changes made)
file patent files\patent1972.dta saved
(551682 real changes made)
file patent files\patent1973.dta saved
(551682 real changes made)
file patent files\patent1974.dta saved
(551682 real changes made)
file patent files\patent1975.dta saved
(551682 real changes made)
file patent files\patent1976.dta saved
(551682 real changes made)
file patent files\patent1977.dta saved
(551682 real changes made)
file patent files\patent1978.dta saved
(551682 real changes made)
file patent files\patent1979.dta saved
(551682 real changes made)
file patent files\patent1980.dta saved
(551682 real changes made)
file patent files\patent1981.dta saved
(551682 real changes made)
file patent files\patent1982.dta saved
(551682 real changes made)
file patent files\patent1983.dta saved
(551682 real changes made)
file patent files\patent1984.dta saved
(551682 real changes made)
file patent files\patent1985.dta saved
(551682 real changes made)
file patent files\patent1986.dta saved
(551682 real changes made)
file patent files\patent1987.dta saved
(551682 real changes made)
file patent files\patent1988.dta saved
(551682 real changes made)
file patent files\patent1989.dta saved
(551682 real changes made)
file patent files\patent1990.dta saved
(551682 real changes made)
file patent files\patent1991.dta saved
(551682 real changes made)
file patent files\patent1992.dta saved
(551682 real changes made)
file patent files\patent1993.dta saved
(551682 real changes made)
file patent files\patent1994.dta saved
(551682 real changes made)
file patent files\patent1995.dta saved
(551682 real changes made)
file patent files\patent1996.dta saved
(551682 real changes made)
file patent files\patent1997.dta saved
(551682 real changes made)
file patent files\patent1998.dta saved
(551682 real changes made)
file patent files\patent1999.dta saved

. use "patent files\patent1969.dta", clear

. append using "patent files\patent1970.dta"

. append using "patent files\patent1971.dta"

. append using "patent files\patent1972.dta"

. append using "patent files\patent1973.dta"

. append using "patent files\patent1974.dta"

. append using "patent files\patent1975.dta"

. append using "patent files\patent1976.dta"

. append using "patent files\patent1977.dta"

. append using "patent files\patent1978.dta"

. append using "patent files\patent1979.dta"

. append using "patent files\patent1980.dta"

. append using "patent files\patent1981.dta"

. append using "patent files\patent1982.dta"

. append using "patent files\patent1983.dta"

. append using "patent files\patent1984.dta"

. append using "patent files\patent1985.dta"

. append using "patent files\patent1986.dta"

. append using "patent files\patent1987.dta"

. append using "patent files\patent1988.dta"

. append using "patent files\patent1989.dta"

. append using "patent files\patent1990.dta"

. append using "patent files\patent1991.dta"

. append using "patent files\patent1992.dta"

. append using "patent files\patent1993.dta"

. append using "patent files\patent1994.dta"

. append using "patent files\patent1995.dta"

. append using "patent files\patent1996.dta"

. append using "patent files\patent1997.dta"

. append using "patent files\patent1998.dta"

. append using "patent files\patent1999.dta"

. 
. save "patent files\raw_patent_panel.dta", replace
file patent files\raw_patent_panel.dta saved

. 
. 
. *************
. * generate patent dummy
. *************
. 
. gen pat=0

. replace pat=1 if year==gyear
(551682 real changes made)

. 
. so ticker year

. egen firmnum=group(ticker)

. 
. gen numpatents2=numpatentsy if pat==1
(16550460 missing values generated)

. 
. 
. *************
. * Reduce the blown up data to one row per company and year.
. * These commands finally create the patent panel.
. *************
. 
. so firmnum year

. count if firmnum==firmnum[_n-1] & year==year[_n-1]
17055270

. drop if firmnum==firmnum[_n-1] & year==year[_n-1] & pat==0
(16505113 observations deleted)

. drop if firmnum==firmnum[_n+1] & year==year[_n+1] & pat==pat[_n+1]
(529644 observations deleted)

. drop if firmnum==firmnum[_n+1] & year==year[_n+1] & pat==0 & pat[_n+1]==1
(20513 observations deleted)

. 
. drop gyear numpatentsy pat firmnum

. rename numpatents2 numpatentsy

. replace numpatentsy=0 if numpatentsy==.
(24834 real changes made)

. label var numpatentsy "number of patents per firm/year"

.  
. so ticker year

. 
. gen patents=numpatentsy

. by ticker: replace patents=numpatentsy+patents[_n-1]*(1-0.15) if year>1969
(35907 real changes made)

. label var patents "patentstock, depreciated"

. 
. by ticker: gen patents_t_1=patents[_n-1]
(1512 missing values generated)

. by ticker: gen patents_t_2=patents[_n-2]
(3024 missing values generated)

. by ticker: gen patents_t_3=patents[_n-3]
(4536 missing values generated)

. by ticker: gen patents_t_4=patents[_n-4]
(6048 missing values generated)

. label var patents_t_1 "patentstock, lag 1"

. label var patents_t_2 "patentstock, lag 2"

. label var patents_t_3 "patentstock, lag 3"

. label var patents_t_4 "patentstock, lag 4"

. 
. replace patents=0 if patents==.
(0 real changes made)

. replace patents_t_1=0 if patents_t_1==.
(1512 real changes made)

. replace patents_t_2=0 if patents_t_2==.
(3024 real changes made)

. replace patents_t_3=0 if patents_t_3==.
(4536 real changes made)

. 
. sort ticker year

. 
. save patentpanel.dta, replace
file patentpanel.dta saved

. 
. log close
      name:  <unnamed>
       log:  E:\REStat_MS14767_Vol96(2)\Data preparation Compustat\11_patentpanel.log
  log type:  text
 closed on:  18 Dec 2014, 17:02:14
------------------------------------------------------------------------------------------------------------------------------------------
